package com.example.demo.service;

import com.example.demo.entity.Customer;
import com.example.demo.repository.CustomerReposity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.core.Converter;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CustomerService implements BaseService<Customer> {
    @Autowired
    CustomerReposity customerReposity;


    @Override
    public Customer findByID(int id) {
        return customerReposity.findById(id).get();
    }

    @Override
    public List<Customer> findAll() {
        return customerReposity.findAll();
    }

    @Override
    public Customer save(Customer customer) {
        return customerReposity.save(customer);
    }

    @Override
    public boolean deleteByid(int id) {
        customerReposity.deleteById(id);
        boolean present = customerReposity.findById(id).isPresent();
        return !present;
    }

    @Override
    public Customer updateById(Customer customer) {
        Customer customer1 = customerReposity.findById(customer.getCustomerId()).get();
        customer1.setPhonenumber(customer.getPhonenumber());
        Customer save = customerReposity.save(customer1);
        return save;
    }

    @Override
    public Page<Customer> findAllByPage(int pageNum) {
        PageRequest of = PageRequest.of(pageNum, 5);
        Page<Customer> all = customerReposity.findAll(of);
        return all;
    }


}
